<?php

namespace addons\sum\controller;

use addons\sum\controller\Common;
use app\admin\model\sum\Goods as GoodsModel;

class Goods extends Common
{

    protected $noNeedLogin = ['index'];
    protected $noNeedRight = ['*'];

    public function index()
    {
        // 使用 PHP 的反射 API 获取 模型 protected 属性
        // 获取多语言的字段
        $obj=new GoodsModel();
        $reflection = new \ReflectionClass($obj);
        $property = $reflection->getProperty('lang_field');
        $property->setAccessible(true);
        $lang_field=$property->getValue($obj);
        // 获取当前语言标识
        $langSlug=$this->langSlug;

        $list_rows = $this->request->param('list_rows', 10);
        $records=GoodsModel::where('status','normal')->paginate($list_rows);
        $records->each(function ($record)use($lang_field,$langSlug){
            foreach ($lang_field as $lfv){
                $record[$lfv]=$record['lang_data'][$langSlug][$lfv]?$record['lang_data'][$langSlug][$lfv]:$record[$lfv];
            }
            $record->hidden(['image','lang_data']);
        });
        $this->success(__('Success'),$records);
    }

}
